/*
Simple Forum Auto Poster.
Copyright (C) 2011 Gianni Van Hoecke.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package domain.xml;

import domain.DomainController;
import domain.forumdata.Forum;
import domain.forumdata.ForumCategory;
import java.io.StringWriter;
import java.util.logging.Level;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 *
 * @author Gianni Van Hoecke <gianni.vh@gmail.com>
 */
public class ForumExporter
{
    /**
     * @return All forums in XML format.
     * <strong>ATTENTION</strong>: the password will be visible!
     */
    public static String forumsToXML()
    {
        try
        {
            //Create the Document
            DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
            Document doc = docBuilder.newDocument();

            //Create the root element and add it to the document.
            Element root = doc.createElement("forums");
            doc.appendChild(root);

            //Create the child elements, and add to root.
            for(Forum forum : DomainController.getInstance().getForums().getForums())
            {
                //The forum child.
                Element forumNode = doc.createElement("forum");
                forumNode.setAttribute("name", forum.getForumName());
                root.appendChild(forumNode);

                //The settings of the forum.
                Element e = doc.createElement("type");
                e.appendChild(doc.createTextNode(forum.getForumType()));
                forumNode.appendChild(e);

                e = doc.createElement("loginURL");
                e.appendChild(doc.createTextNode(forum.getLoginURL()));
                forumNode.appendChild(e);

                e = doc.createElement("username");
                e.appendChild(doc.createTextNode(forum.getUsername()));
                forumNode.appendChild(e);

                e = doc.createElement("password");
                e.appendChild(doc.createTextNode(new String(forum.getPassword())));
                forumNode.appendChild(e);

                //The categories.
                Element categories = doc.createElement("categories");
                forumNode.appendChild(categories);
                
                for(ForumCategory fc : forum.getCategories())
                {
                    Element category = doc.createElement("category");
                    category.setAttribute("name", fc.getCategory().getName());
                    category.setAttribute("url", fc.getUrl());
                    categories.appendChild(category);
                }
            }

            //Set up the transformer.
            TransformerFactory transfac = TransformerFactory.newInstance();
            Transformer trans = transfac.newTransformer();
            trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            trans.setOutputProperty(OutputKeys.INDENT, "yes");

            //Create the string from the xml tree.
            StringWriter sw = new StringWriter();
            trans.transform(new DOMSource(doc), new StreamResult(sw));
            
            return sw.toString();
        }
        catch(Exception e)
        {
            DomainController.getInstance().getLogger().log(Level.SEVERE, "Can't export forums: {0}", e.getMessage());
            return "";
        }
    }
}